package com.yanqu.road.server.cmd.activity.tonbing.cross;

import com.yanqu.road.entity.activity.tonbing.config.TonBingConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TonBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_TON_BING_MOVE_SPEED_UP_FROM_CROSS, desc = "")
public class TonBingMoveSpeedUpFromCrossCmd extends AbstractCommand {

    private TonBingProto.TonBingMoveSpeedUpRespMsg resp;
    private int realTimes;
    private int reqTimes;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TonBingProto.CrossTonBingMoveSpeedUpRespMsg respMsg = TonBingProto.CrossTonBingMoveSpeedUpRespMsg.parseFrom(packet.getBytes());
        resp = respMsg.getResp();
        realTimes = respMsg.getRealTimes();
        if (respMsg.hasRealTimes()) {
            reqTimes = respMsg.getReqTimes();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TonBingConfig config = TonBingMgr.getConfig();

        // 预扣除返还
        Property preCost = PropertyHelper.parseStringToProperty(config.getShenXingItem() + "=" + reqTimes);
        player.getModule(PreReduceModule.class).restorePreReduce(preCost);

        // 成功了要扣消耗
        if (resp.getRet() == 0) {
            Property cost = PropertyHelper.parseStringToProperty(config.getShenXingItem() + "=" + realTimes);
            player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.TonBing, eLogMoneyType.TonBingMoveSpeedUp);
        }

        // 返回
        player.sendPacket(ClientProtocol.U_TON_BING_MOVE_SPEED_UP, resp.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
